//
//  TimePicker.m
//  PhnixHome
//
//  Created by Levy Xu on 2017/11/28.
//  Copyright © 2017年 Levy Xu. All rights reserved.
//

#import "TimePicker.h"

@interface TimePicker()<UIPickerViewDelegate, UIPickerViewDataSource>
@property(nonatomic, strong) UISwitch *sw;
@property(nonatomic, strong) UIPickerView *pickerView;
@property(nonatomic, strong) QMUILabel *hourLabel;
@property(nonatomic, strong) QMUILabel *minuteLabel;

//@property(nonatomic, strong) QMUILabel *startLabel;
//@property(nonatomic, strong) QMUILabel *endLabel;
@end

@implementation TimePicker

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = UIColorClear;
        self.contentView.backgroundColor = UIColorWhite;
        [self.contentView showCornerRadius];
        [self.contentView showShadow];
        
        self.textLabel.font = CodeFontMake(16);
        self.textLabel.textColor = UIColorGray4;
        
        _sw = [[UISwitch alloc] init];
        _sw.onTintColor = GQDThemeManager.currentTheme.themeTintColor;
        [_sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:_sw];
        
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;
        [self.contentView addSubview:_pickerView];
        
        _hourLabel = [[QMUILabel alloc] init];
        _hourLabel.font = CodeFontMake(14);
        _hourLabel.textColor = UIColorGray4;
        _hourLabel.textAlignment = NSTextAlignmentCenter;
        _hourLabel.text = NSLocalizedString(@"hour", nil);
        [_hourLabel sizeToFit];
        [_pickerView addSubview:_hourLabel];
        
        _minuteLabel = [[QMUILabel alloc] init];
        [_minuteLabel qmui_setTheSameAppearanceAsLabel:_hourLabel];
        _minuteLabel.text = NSLocalizedString(@"minute", nil);
        [_minuteLabel sizeToFit];
        [_pickerView addSubview:_minuteLabel];
        
//        _pickerView.qmui_borderPosition = QMUIBorderViewPositionBottom;
//
//        _startLabel = [[QMUILabel alloc] init];
//        _startLabel.font = CodeFontMake(14);
//        _startLabel.textColor = UIColorGray4;
//        _startLabel.textAlignment = NSTextAlignmentCenter;
//        [self.contentView addSubview:_startLabel];
//
//        _endLabel = [[QMUILabel alloc] init];
//        [_endLabel qmui_setTheSameAppearanceAsLabel:_startLabel];
//        [self.contentView addSubview:_endLabel];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    
    self.contentView.frame = CGRectInset(self.contentView.frame, 10, 0);
    
    self.textLabel.frame = CGRectMake(10, 10, 200, 20);
    
    _sw.frame = CGRectMake(self.contentView.qmui_width - 61, 10, 51, 30);
    
    _pickerView.frame = CGRectMake(30, 40, self.contentView.qmui_width - 60, self.contentView.qmui_height - 50);
    _hourLabel.frame = CGRectMake(_pickerView.qmui_width / 2 - 40.0, _pickerView.qmui_height / 2.0 - 10, 40, 20);
    _minuteLabel.frame = CGRectMake(_pickerView.qmui_width / 2 + 60.0, _pickerView.qmui_height / 2.0 - 10, 40, 20);
    
//    _startLabel.frame = CGRectMake(5, _pickerView.qmui_bottom + 5, self.contentView.qmui_width / 2 - 10, 20);
//    _endLabel.frame = CGRectMake(_startLabel.qmui_right, _startLabel.qmui_top, _startLabel.qmui_width, 20);
}

-(void)setHour:(NSInteger)hour {
    _hour = hour;
    [_pickerView selectRow:_hour inComponent:0 animated:YES];
    
//    [self displayBottom];
}

-(void)setMinute:(NSInteger)minute {
    _minute = minute;
    [_pickerView selectRow:_minute inComponent:1 animated:YES];
    
//    [self displayBottom];
}

-(void)setDisabled:(BOOL)disabled {
    _disabled = disabled;
    
    [_sw setOn:!disabled];
    _pickerView.userInteractionEnabled = !disabled;
    [_pickerView reloadAllComponents];
    
//    [self displayBottom];
}

-(void)switchAction:(id)sender {
    self.disabled = !_sw.isOn;
}

-(NSInteger)onTime {
    if (_sw.isOn) {
        NSInteger result = 60 *(_hour - 3) + _minute;
        if (result < 0) {
            result += 24 * 60;
        }
        return result;
    }
    return 0xFFFF;
}

-(NSInteger)offTime {
    if (_sw.isOn) {
        NSInteger result = 60 *(_hour + 1) + _minute;
        if (result >= 24 * 60) {
            result -= 24 * 60;
        }
        return result;
    }
    return 0xFFFF;
}

//-(void)displayBottom {
//    if (_sw.isOn) {
//        int ontime = (int)[self onTime];
//        int offtime = (int)[self offTime];
//        _startLabel.text = [NSString stringWithFormat:@"开机时间：%d:%02d",ontime / 60, ontime % 60];
//        _endLabel.text = [NSString stringWithFormat:@"关机时间：%d:%02d",offtime / 60, offtime % 60];
//    } else {
//        _startLabel.text = @"开机时间：未设置";
//        _endLabel.text = @"关机时间：未设置";
//    }
//}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return 24;
    } else {
        return 60;
    }
}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40.0f;
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 100.0f;
}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    QMUILabel *customLabel = (QMUILabel *)view;
    if (!customLabel) {
        customLabel = [[QMUILabel alloc] init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        customLabel.font = CodeFontMake(25);
        customLabel.contentEdgeInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    }
    customLabel.textColor = self.disabled ? UIColorGray5 : UIColorGray1;
    customLabel.text = [NSString stringWithFormat:@"%02ld",(long)row];
    return customLabel;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _hour = row;
    } else {
        _minute = row;
    }
//    [self displayBottom];
}
@end
